<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="家长姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" :maxLength="10" placeholder="请输入家长姓名"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="家长性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sex">
              <j-dict-select-tag type="list" v-model="model.sex" dictCode="sex" placeholder="请选择家长性别" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" placeholder="请输入家长联系电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学生姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentIds">
              <j-multi-select-tag v-model="model.studentIds" dictCode="sk_student,name,id,del_flag=0" placeholder="请选择学生">
              </j-multi-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="与学生关系" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="relation">
              <j-dict-select-tag
                type="list"
                v-model="model.relation"
                dictCode="kinship"
                placeholder="请选择家长与学生的关系"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="证件类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idType">
              <j-dict-select-tag
                type="list"
                v-model="model.idType"
                dictCode="certificate_type"
                placeholder="请选择家长证件类型"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="证件号码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="idCode">
              <a-input v-model="model.idCode" :maxLength="30" placeholder="请输入家长证件号码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="家长照片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="picture">
              <j-image-upload :number="1" isMultiple   v-model="model.picture"></j-image-upload>
              <p>请上传 1 张<span style="margin:0 5px;color:#f00;">2寸</span>照片</p>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { validateMobile } from '@/utils/rules.js'
export default {
  name: 'SkGuardianForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        name: [{ required: true, message: '请输入家长姓名' }],
        sex: [{ required: true, message: '请选择家长性别' }],
        studentIds: [{ required: true, message: '请选择接送学生' }],
        relation: [{ required: true, message: '请选择家长与学生的关系' }],
        phone: [
          { required: true, message: '请输入家长联系电话' },
          { validator: validateMobile }
        ],
        picture: [{ required: true, message: '请上传家长照片' }],
        idType: [{ required: true, message: '请选择家长证件类型' }],
        idCode: [{ required: true, message: '请输入家长证件号码' }]
      },
      url: {
        add: '/management/skGuardian/add',
        edit: '/management/skGuardian/edit',
        queryById: '/management/skGuardian/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          this.$emit('modelLoading', that.confirmLoading)
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          this.model.selectedstudents = this.model.studentIds
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              this.$emit('modelLoading', that.confirmLoading)
            })
        }
      })
    }
  }
}
</script>
